package com.lzw.blog.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzw.blog.Service.LoginService;
import com.lzw.blog.Service.UserService;
import com.lzw.blog.dao.mapper.UserMapper;
import com.lzw.blog.dao.mapper.UserRoleMspper;
import com.lzw.blog.dao.pojo.User;
import com.lzw.blog.dao.pojo.UserRole;
import com.lzw.blog.vo.ErrorCode;
import com.lzw.blog.vo.LoginUserVo;
import com.lzw.blog.vo.Result;
import com.lzw.blog.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginService loginService;

    @Autowired
    private UserRoleMspper userRoleMspper;

    @Override
    public UserVo findUserVoById(Long authorId) {
        User user = userMapper.selectById(authorId);
        if (user == null) {
            user = new User();
            user.setId(1L);
            user.setAvatar("/static/img/logo.b3a48c0.png");
            user.setNickname("kabuqinuo");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public User findUserById(Long authorId) {
        User user = userMapper.selectById(authorId);
        if (user == null) {
            user = new User();
            user.setNickname("kabuqinuo");
        }
        return user;
    }

    @Override
    public User findUser(String account, String password) {
        //查询条件,根据用户名和加密后的密码来进行判断
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, account);
        queryWrapper.eq(User::getPassword, password);
        queryWrapper.select(User::getId, User::getAccount, User::getAvatar, User::getNickname);
        //提高查询效率
        queryWrapper.last("limit 1");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Result findUserBytoken(String token) {
        /**
         * 1.token合法性校验
         * 是否为空,解析是否成功，redis是否存在
         * 2.校验失败,返回信息
         * 3.如果解析成功返回vo对象,LoginVo
         */
        //校验token
        User user = loginService.CheckToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(user.getId());
        loginUserVo.setNickname(user.getNickname());
        loginUserVo.setAccount(user.getAccount());
        loginUserVo.setAvatar(user.getAvatar());
        return Result.success(loginUserVo);
    }

    @Override
    public User findUserByAccount(String account) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, account);
        queryWrapper.last("limit 1");
        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findUserByEamil(String email) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, email);
        queryWrapper.last("limit 1");
        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(User user) {

        this.userMapper.insert(user);
        user = findUserByAccount(user.getAccount());
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(1L);
        userRoleMspper.insert(userRole);

    }
}
